package builder.AccountFormBuilderPackage;

import java.util.ArrayList;

import handler.assetFormHandlers.AFTextFieldKeyListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import builder.FormBuilder;

import other.CodeDescriptionPair;

import checker.ValidLengthAndCharChecker;
import form.FormComboBox;
import form.FormTextField;
import form.AccountForm.AccountForm;

public class AccountFormBuilder extends FormBuilder {

	public AccountFormBuilder() {
		super(new AccountForm());
	}

	@Override
	public void setFormSettings() {
		 		
	}

	@Override
	public void buildComponents() {
		AccountForm accountForm = (AccountForm) form;
		accountForm.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
		buildUsernameTF(accountForm);
		buildPasswordTF(accountForm);
		buildAcctType(accountForm);
		setInvisible(accountForm);
		accountForm.addComponents();
	}
	
	public void buildUsernameTF(AccountForm accountForm){
		JTextField userName = new JTextField();
		userName.setColumns(10);
		FormTextField UsernameTF = new FormTextField("User Name:", userName, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHANUMERIC,20));
		accountForm.setUserName(UsernameTF);
		userName.addKeyListener(new AFTextFieldKeyListener(UsernameTF));
		accountForm.add(UsernameTF);
	}
	
	public void buildPasswordTF(AccountForm accountForm){
		JPasswordField password = new JPasswordField();
		password.setColumns(10);
		FormTextField PasswordTF = new FormTextField("Password:", password, new ValidLengthAndCharChecker(ValidLengthAndCharChecker.ALPHANUMERIC,20));
		accountForm.setPassword(PasswordTF);
		password.addKeyListener(new AFTextFieldKeyListener(PasswordTF));
		accountForm.add(PasswordTF);
	}
	
	public void buildAcctType(AccountForm accountForm){
		JComboBox<String> comboAcctType = new JComboBox<String>();
		initComboBoxChoices(comboAcctType, CodeDescriptionPair.TABLE_USER_TYPES);
		FormComboBox AcctTypeCB = new FormComboBox("User Type:", comboAcctType, null);
		accountForm.setAcctType(AcctTypeCB);
		accountForm.add(AcctTypeCB);
	}
	
	private void initComboBoxChoices(JComboBox<String> comboBox, String refTableName){
		ArrayList<CodeDescriptionPair> typeCDP = CodeDescriptionPair.getCodeDescriptionPairsFor(refTableName);
		
		for(int i=0;i<typeCDP.size();i++)
			comboBox.addItem(typeCDP.get(i).getDescription());
	}
	
	private void setInvisible(AccountForm accountForm){
		accountForm.getAcctType().setVisible(false);
	}
}
